package com.jp.refreshlocalcacheclient.net.netty;

import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

/**
 * @Author: jipeng
 * @Description:  处理器初始化
 * @Date: Created in 2020/5/14 15:47
 */
public class EchoClientHandlerInitializer extends ChannelInitializer<SocketChannel> {

    //消息分割符
    public static String delimiter = "_$";


    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();

        // 这是我们自定义的一个编码器，主要作用是在返回的响应数据最后添加分隔符
        pipeline.addLast(new DelimiterBasedFrameDecoder(1024,
                Unpooled.wrappedBuffer(delimiter.getBytes())));

        pipeline.addLast(new StringDecoder());
        pipeline.addLast(new StringEncoder());
        pipeline.addLast(new EchoClientHandler());
    }
}
